//
//  HandWorkTableviewCell.m
//  HandWork
//
//  Created by 王阳阳 on 16/2/26.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "HandWorkTableviewCell.h"
#import "HandWorkCollectionViewCell.h"

@interface HandWorkTableviewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)UICollectionView *collection;
@end

@implementation HandWorkTableviewCell
-(void)dealloc
{
    [_layout release];
    [_collection release];
    [super dealloc];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creat];
        
    }
    return self;
}
- (void)creat
{
    //头像
    self.touxiangimg = [[UIImageView alloc]init];
    [self.contentView addSubview:self.touxiangimg];
    [self.touxiangimg release];
    
    //名字
    self.name = [[UILabel alloc]init];
    [self.contentView addSubview:self.name];
    [self.name release];
    
    //内容
    self.content = [[UILabel alloc]init];
    [self.contentView addSubview:self.content];
    [self.content release];
    
    
    
    

}
- (void)layoutSubviews
{
    [super layoutSubviews];
    self.touxiangimg.frame = CGRectMake(5, 5, 40, 40);
    
    self.touxiangimg.layer.cornerRadius = 20;
    self.touxiangimg.clipsToBounds = YES;
    
    self.name.frame = CGRectMake(self.touxiangimg.frame.size.width + 10, 10, [UIScreen mainScreen].bounds.size.width - self.touxiangimg.frame.size.width - 15, 30);
    self.name.textColor = [UIColor redColor];
    self.name.font = [UIFont systemFontOfSize:15];
    
    CGFloat h = [[self class]heightWithText:_content.text];
    self.content.frame = CGRectMake(5, self.touxiangimg.frame.size.height + 15, [UIScreen mainScreen].bounds.size.width - 10, h);
    self.content.numberOfLines = 0;
    
    
//    
    CGFloat imgCVH = 0;
    if (_usermodel.arr5.count == 0) {
        imgCVH = 0;
    } else if (_usermodel.arr5.count < 4) {
        imgCVH = (self.contentView.frame.size.width) / 3;
    } else if (_usermodel.arr5.count < 7) {
        imgCVH = (self.contentView.frame.size.width - 10) / 3 * 2;
    } else {
        imgCVH = (self.contentView.frame.size.width - 20);
    }
    

    
    
    
    
//    self.layout = [[UICollectionViewFlowLayout alloc]init];
//    //collectionView
//    self.layout.minimumInteritemSpacing = 0;
//    self.layout.minimumLineSpacing = 0;
//    self.layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 - 5,[UIScreen mainScreen].bounds.size.width / 3 - 5);
//    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50 + h, self.contentView.frame.size.width, imgCVH)collectionViewLayout:_layout];
//    _collection.backgroundColor = [UIColor whiteColor];
//    [self.contentView addSubview:_collection];
//    self.collection.backgroundColor = [UIColor whiteColor];
//    self.collection.delegate = self;
//    self.collection.dataSource = self;
//    self.collection.showsVerticalScrollIndicator = NO;
//    [_collection registerClass:[HandWorkCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//    

    
    self.layout = [[UICollectionViewFlowLayout alloc]init];
   
    self.layout.itemSize = CGSizeMake((self.contentView.frame.size.width - 40)/ 3  , (self.contentView.frame.size.width - 40) / 3 );
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 60 + h, self.contentView.frame.size.width, imgCVH) collectionViewLayout:_layout];
    self.collection.dataSource = self;
    self.collection.delegate = self;
    self.collection.backgroundColor =[UIColor whiteColor];
    self.collection.showsVerticalScrollIndicator = NO;
    self.collection.showsHorizontalScrollIndicator = NO;
    [self.contentView addSubview:self.collection];
    [_collection registerClass:[HandWorkCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    
    
}

+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFileOwnerAccountName:[UIFont systemFontOfSize:17]};
    CGSize size = CGSizeMake(375 - 20, 1000);
    
    CGRect rect = [text boundingRectWithSize:size options:(NSStringDrawingUsesLineFragmentOrigin) attributes:dic context:nil];
    return rect.size.height;
};



#pragma mark - collectionView必须实现的两个方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _usermodel.arr5.count;
//    return 5;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HandWorkCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor redColor];
    [cell.bgimg sd_setImageWithURL:[NSURL URLWithString:_usermodel.arr5[indexPath.item]]];
    return cell;
}
//- (void)setDailyModel:(UserHandWorkModle *)usermodel
//{
//    if (_usermodel != usermodel)
//    {
//        [_usermodel release];
//        _usermodel = [usermodel retain];
//    }
//    [self creatcollection];
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
